package com.zhekou.web.tag;


import com.zhekou.util.CookieUtils;
import com.zhekou.util.JdbcPage;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: fangs
 * Date: 12-10-18
 * Time: 下午6:10
 */
public class PageTag extends TagSupport {
    private int curPage = 1;
    private int totalCount;
    private int pageSize = 5;//显示多少页码
    private int viewCount;//每页显示几条记录
    private JdbcPage page;
    private int style; //第几套样式

    public JdbcPage getPage() {
        return page;
    }

    public void setPage(JdbcPage page) {
        this.page = page;
    }

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getViewCount() {
        return viewCount;
    }

    public void setViewCount(int viewCount) {
        this.viewCount = viewCount;
    }

    public void setStyle(int style) {
        this.style = style;
    }


    public int getStyle() {
        return this.style;
    }


    public int doStartTag() throws JspException {
        if (page == null) return SKIP_BODY;
        totalCount = (int) page.getTotalElements();
        viewCount = page.getPageSize();
        curPage = page.getCurPage();
        if (totalCount <= viewCount) {
            return SKIP_BODY;
        }
        String bar = "";
        try {
            bar += "<div class=\"pagebox\" id=\"PageList_1\">\n";
            bar += getNavigatorBar();
            //输出结束标记
            bar += "</div>";
            pageContext.getOut().write(bar);
            return SKIP_BODY;
        } catch (IOException ioe) {
            throw new JspException(ioe.getMessage());
        }
    }


    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }


    /**
     * 根据指定类型获得导航条预先设计的导航条
     *
     * @return 返回导航条的HTML代码, 若指定类型不存在, 返回""
     */
    private String getNavigatorBar() {
        final String url = getRequestUrl();

        final int totalPage = (totalCount + viewCount - 1) / viewCount;   //总页数
        final int prevOrNextCount = pageSize / 2;

        int firstPosition = curPage - prevOrNextCount;
        int lastPosition = curPage + prevOrNextCount;

        if (firstPosition < 1) {
            firstPosition = 1;
        }
        if (lastPosition > totalPage) {
            lastPosition = totalPage;
        }

        String firstText = ""; //首  页
        String prevText = "";  //上一页
        String nextText = "";  //下一页
        String lastText = "";  //最后一页
        String pagesText = "";//中间的页码

        String first = "首页";
        String last = "尾页";
        String pre = "上一页";
        String next = "下一页";

        if (curPage > 1) {
            firstText = "<span class='pagebox_pre'><a href=\"" + url + "1\">" + first + "</a></span>";
            prevText = "<span class='pagebox_pre'><a href=\"" + url + (curPage - 1) + "\" >" + pre + "</a></span>";
        } else {
            firstText = "<span class='pagebox_pre'><a>" + first + "</a></span>";
            prevText = "<span class='pagebox_pre'><a>" + pre + "</a></span>";
        }
        if (curPage < totalPage) {
            nextText = "<span class='pagebox_pre'><a href=\"" + url + (curPage + 1) + "\" >" + next + "</a></span>";
            lastText = "<span class='pagebox_num'><a href=\"" + url + totalPage + "\" >" + last + "</a></span>\n";
        } else {
            nextText = "<span class='pagebox_pre'><a>" + next + "</a></span>";
            lastText = "<span class='pagebox_num'><a>" + last + "</a></span>";
        }
        for (int i = firstPosition; i <= lastPosition; i++) {
            if (curPage == i) {
                pagesText += "<span class='pagebox_num_nonce'><a>" + i + "</a></span>";
            } else {
                pagesText += "<span class='pagebox_num'><a href=\"" + url + i + "\" >" + i + "</a></span>\n";
            }
        }


        return firstText + " \n " + prevText + " \n " + pagesText + " \n " + nextText + " \n " + lastText + " \n";
    }

    private String getRequestUrl() {
        StringBuilder urlBuilder = new StringBuilder("?");
        Map map = pageContext.getRequest().getParameterMap();

        for (Object o : map.keySet()) {
            String key = (String) o;
            String value = ((String[]) map.get(key))[0];
            try {
                value = URLEncoder.encode(value, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            if (!key.trim().equalsIgnoreCase("curPage")) {
                urlBuilder.append(key).append("=").append(value).append("&");
            }
        }
        urlBuilder.append("curPage=");

        return urlBuilder.toString();
    }
}
